package org.example.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.HeadlineMapper;
import org.example.pojo.Headline;
import org.example.pojo.vo.PortalVo;
import org.example.service.HeadlineService;
import org.example.utils.JwtHelper;
import org.example.utils.Result;
import org.example.utils.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋孟林
 * @description
 */
@Transactional
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline> implements HeadlineService {
    @Autowired
    private HeadlineMapper headlineMapper;
    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 分页查询新闻
     * @param portalVo 1
     * @return 1
     * @author 宋孟林
     */
    @Override
    public Result<Object> findNewsPage(PortalVo portalVo) {
        Page<Map<Object, Object>> page = new Page<>();
        headlineMapper.selectPageMap(page, portalVo);
        List<Map<Object, Object>> records = page.getRecords();
        Map<Object, Object> map = new HashMap<>();
        map.put("pageData", records);
        map.put("pageNum", page.getCurrent());
        map.put("pageSize", page.getSize());
        map.put("totalPage", page.getPages());
        map.put("totalSize", page.getTotal());
        Map<Object, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo", map);
        return Result.ok(pageInfo);
    }

    /**
     * 根据新闻id显示新闻详情
     * 查询玩详情后使用mybatis-plus的update方法将阅读量+1
     * @param hid 新闻id
     * @return 新闻详情
     * @author 宋孟林
     */
    @Override
    public Result<Object> findHeadlineVo(int hid) {
       Map<Object, Object> data = headlineMapper.findHeadlineVo(hid);
       LambdaUpdateWrapper<Headline> wrapper = new LambdaUpdateWrapper<>();
       wrapper.set(Headline::getPageViews, ((Integer)data.get("pageViews")) + 1).eq(Headline::getHid, hid);
       headlineMapper.update(null, wrapper);
       Map<Object, Object> headlineMap = new HashMap<>();
       headlineMap.put("headline", data);
       return Result.ok(headlineMap);
    }

    @Override
    public Result<Object> publish(Headline headline, String token) {
        if (StringUtils.isEmpty(token) || jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        Headline headline1 = new Headline();
        BeanUtils.copyProperties(headline, headline1);
        headline1.setPublisher(userId);
        headline1.setPageViews(0);
        headline1.setCreateTime(LocalDateTime.now());
        headline1.setUpdateTime(LocalDateTime.now());
        boolean flag = this.save(headline1);
        if (flag){
            return Result.ok(null);
        }else
            return Result.build(null, 500, "插入失败");
    }

    @Override
    public Result<Object> findHeadlineByHid(int hid, String token) {
        if (StringUtils.isEmpty(token) || jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        Headline headline = headlineMapper.selectById(hid);
        Map<Object, Object> map = new HashMap<>();
        map.put("headline", headline);
        return Result.ok(map);
    }

    @Override
    public Result<Object> CustomUpdate(Headline headline, String token) {
        if (StringUtils.isEmpty(token) || jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        headline.setUpdateTime(LocalDateTime.now());
        headline.setPublisher(userId);
        boolean flag = this.updateById(headline);
        if (flag){
            return Result.ok(null);
        }else
            return Result.build(null, 500, "更新失败");
    }
}

